﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model;
using BLL;
using System.Data;

public partial class Admin : System.Web.UI.Page
{

    private List<House> houseList;
    public List<House> HouseList
    {
        get { return HouseManage.GetHousesChecked(); }
        set { houseList = value; }
    }

    private List<PersonalInformation> infoList;
    public List<PersonalInformation> InfoList
    {
        get 
        {
            List<PersonalInformation> pList = new List<PersonalInformation>();
            List<HouseHolder> list = HouseHolderManage.GetHouseHoldersChecked();
            foreach (HouseHolder hHolder in list)
            {
                int pId = hHolder.PersonalInformationId;
                PersonalInformation info = PersonalInformationManage.GetPersonalInformationById(pId);
                pList.Add(info);
            }
            return pList;
        }
        set { infoList = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
            return;
        if (Session["UserType"] == null || Convert.ToInt32(Session["UserType"]) != 2)
            Response.Redirect("Login.aspx");

        Bind();
    }

    private void Bind()
    {
        BindHouseNotChecked();
        BindHHolderNotChecked();
        BindHouseRepeater();
        BindHolderRepeater();
    }

    private void BindHouseRepeater()
    {
        this.rptHouseManage.DataSource = HouseList;
        this.rptHouseManage.DataBind();
    }
    private void BindHolderRepeater()
    {
        this.rptHolderManage.DataSource = InfoList;
        this.rptHolderManage.DataBind();
    }

    private void BindHHolderNotChecked()
    {
        List<HouseHolder> list = HouseHolderManage.GetHouseHoldersNotChecked();
        DataTable dt = new DataTable();
        dt.Columns.Add("Id", typeof(int));
        dt.Columns.Add(new DataColumn("姓名"));
        dt.Columns.Add(new DataColumn("昵称"));
        dt.Columns.Add(new DataColumn("邮箱"));
        dt.Columns.Add(new DataColumn("手机"));
        dt.Columns.Add(new DataColumn("性别"));
        dt.Columns.Add(new DataColumn("用户类型"));
        for (int cnt = 0; cnt < list.Count; cnt++)
        {
            DataRow row = dt.NewRow();
            PersonalInformation info = PersonalInformationManage.GetPersonalInformationById(list[cnt].PersonalInformationId);
            row["Id"] = list[cnt].Id;
            row["姓名"] = info.Name;
            row["昵称"] = info.Nickname;
            row["邮箱"] = info.Email;
            row["手机"] = info.Phone;
            row["性别"] = ((GenderName)info.Gender).ToString();
            row["用户类型"] = ((UserTypeName)0).ToString();
            dt.Rows.Add(row);
        }
        Session["HouseHolderNotCheckedTable"] = dt;
        this.gvHHoldersNotChecked.DataSource = dt;
        this.gvHHoldersNotChecked.DataBind();
    }

    private void BindHouseNotChecked()
    {
        List<House> list = HouseManage.GetHousesNotChecked();
        DataTable dt = new DataTable();
        dt.Columns.Add("Id", typeof(int));
        dt.Columns.Add(new DataColumn("地址"));
        dt.Columns.Add(new DataColumn("出租方式"));
        dt.Columns.Add(new DataColumn("房屋类型"));
        dt.Columns.Add(new DataColumn("出租时间"));
        dt.Columns.Add(new DataColumn("截止日期"));
        dt.Columns.Add(new DataColumn("最晚入住时间"));
        dt.Columns.Add(new DataColumn("价格"));
        dt.Columns.Add(new DataColumn("规格"));
        dt.Columns.Add(new DataColumn("房间数量"));

        for (int cnt = 0; cnt < list.Count; cnt++)
        {
            DataRow row = dt.NewRow();
            row["Id"] = list[cnt].Id;
            row["地址"] = list[cnt].Address;
            row["出租方式"] = ((LeaseModeName)list[cnt].LeaseModeId).ToString();
            row["房屋类型"] = ((HouseTypeName)list[cnt].HouseTypeId).ToString();
            row["出租时间"] = list[cnt].Duration.ToString();
            row["截止日期"] = list[cnt].ExpirationDate.ToString();
            row["最晚入住时间"] = list[cnt].IntakeTime.ToString();
            row["价格"] = list[cnt].Price.ToString();
            row["规格"] = list[cnt].Standard;
            row["房间数量"] = list[cnt].RoomNumber.ToString();
            
            dt.Rows.Add(row);
        }
        Session["HouseNotCheckedTable"] = dt;
        this.gvHouseNotChecked.DataSource = dt;
        this.gvHouseNotChecked.DataBind();
    }

    
    protected void HouseHolderChecked(object sender, GridViewUpdateEventArgs e)
    {
        int id = e.RowIndex;
        DataTable dt = (DataTable)Session["HouseHolderNotCheckedTable"];
        DataRow row = dt.Rows[id];
        HouseHolderManage.Checked(Convert.ToInt32(row["Id"]), 1);
        Bind();
    }
    protected void HouseChecked(object sender, GridViewUpdateEventArgs e)
    {
        int id = e.RowIndex;
        DataTable dt = (DataTable)Session["HouseNotCheckedTable"];
        DataRow row = dt.Rows[id];
        HouseManage.Checked(Convert.ToInt32(row["Id"]),1);
        Bind();
    }
    protected void HouseManageClick(object sender, EventArgs e)
    {
         // 更改Rating Review Recommend
        UpdateHouseRating(this.rptHouseManage);
        UpdateHouseReview(this.rptHouseManage);
        UpdateHouseRecommend(this.rptHouseManage);
        Bind();
    }

    protected void HolderManageClick(object sender, EventArgs e)
    {
        UpdateHolderRecommend(this.rptHolderManage);
        UpdateHolderRating(this.rptHolderManage);
        Bind();
    }

    private void UpdateHouseRating(Control control)
    {
        if (control is TextBox)
        {
            TextBox tb = (TextBox)control;
            if (tb.Attributes["tbType"] == "Rating")
            {
                int id = Convert.ToInt32(tb.Attributes["HouseId"]);
                tb.Text = (tb.Text == "" ? "0" : tb.Text);
                HouseManage.Rating(id, Convert.ToInt32(tb.Text));
            }
        }
        else if(control.HasControls())
        {
            foreach (Control c in control.Controls)
            {
                UpdateHouseRating(c);
            }
        }
    }

    private void UpdateHouseReview(Control control)
    {
        if (control is TextBox)
        {
            TextBox tb = (TextBox)control;
            if (tb.Attributes["tbType"] == "AdminReview")
            {
                int id = Convert.ToInt32(tb.Attributes["HouseId"]);
                HouseManage.AdminReview(id, tb.Text);
            }
        }
        else if (control.HasControls())
        {
            foreach (Control c in control.Controls)
            {
                UpdateHouseReview(c);
            }
        }
    }

    private void UpdateHouseRecommend(Control control)
    {
        if (control is CheckBox)
        {
            CheckBox cb = (CheckBox)control;
            if (cb.Attributes["cbType"] == "Recommend")
            {
                int id = Convert.ToInt32(cb.Attributes["HouseId"]);
                HouseManage.Recommend (id, cb.Checked);
            }
        }
        else if (control.HasControls())
        {
            foreach (Control c in control.Controls)
            {
                UpdateHouseRecommend(c);
            }
        }
    }

    private void UpdateHolderRecommend(Control control)
    {
        if (control is CheckBox)
        {
            CheckBox cb = (CheckBox)control;
            if (cb.Attributes["cbType"] == "Recommend")
            {
                int id = Convert.ToInt32(cb.Attributes["HolderId"]);
                HouseHolderManage.Recommend(id, cb.Checked);
            }
        }
        else if (control.HasControls())
        {
            foreach (Control c in control.Controls)
            {
                UpdateHolderRecommend(c);
            }
        }
    }

    private void UpdateHolderRating(Control control)
    {
        if (control is TextBox)
        {
            TextBox tb = (TextBox)control;
            if (tb.Attributes["tbType"] == "Rating")
            {
                int id = Convert.ToInt32(tb.Attributes["HolderId"]);
                tb.Text = (tb.Text == "" ? "0" : tb.Text);
                HouseHolderManage.Rating(id, Convert.ToInt32(tb.Text));
            }
        }
        else if (control.HasControls())
        {
            foreach (Control c in control.Controls)
            {
                UpdateHolderRating(c);
            }
        }
    }
}